home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2003 June / macformat-130.iso / pc / Shareware_Freeware (OS X) / Internet / soybo-1.0.1 / macosx_components / Sample Soyvices / Filesystem / Filesystem.php next >
Encoding:
PHP Script  |  2003-03-27  |  4.8 KB  |  189 lines

  1. <?php
  2.  
  3. /* Copyright (C) 2003 Adam Tow
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2 of the License, or
  8.    (at your option) any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  18.    
  19.  
  20. function RunWithParams($methodName, $params)
  21. {    
  22.     $servicePath = $params["path_service"];
  23.         
  24.     include_once $servicePath . "support/mime_lookup.php";
  25.     include_once $servicePath . "support/unicodeToEntities.php";
  26.  
  27.     switch($methodName)
  28.     {
  29.         case "file":    return EditFile($params);
  30.                         break;
  31.         
  32.         case "cd":        
  33.         default:        return ChangeDirectory($params);
  34.                         break;
  35.     
  36.     
  37.     }
  38. }
  39.  
  40. function ChangeDirectory($params)
  41. {
  42.     $directory = $params["dir"];
  43.     $showHidden = $params["showHidden"];
  44.     
  45.     if(!$directory)
  46.         $directory = "/";
  47.     
  48.     //echo_object(0, $params, "Parameters");
  49.  
  50.     if(file_exists($directory) && is_dir($directory)) {
  51.         $directory = realpath($directory);
  52.         
  53.         $len = strlen($directory);
  54.         
  55.         if(is_dir($directory) && $directory[$len - 1] != "/")
  56.             $directory .= "/";
  57.         
  58.         $dir = @dir($directory);
  59.         $directories = array();
  60.         $files = array();
  61.     
  62.         //$userInfo = posix_getpwnam("adam");
  63.         $userInfo = posix_getlogin();
  64.     
  65.         while($dir && false !== ($file = $dir->read())) {
  66.             if($file != "." && $file != "..") {
  67.                 $type = @filetype(realpath($directory . $file));
  68.                 
  69.                 
  70.                     // Only handle directories and files
  71.                 
  72.                 $appPos = strpos($file, ".app");
  73.                 $len = strlen($file);
  74.  
  75.                 $filename = utf8ToUnicodeEntities($file);
  76.                 //$filename = $file;
  77.                 //echo $filename . "<br />";
  78.                 
  79.                 if($type == "dir" && (!$appPos || $appPos != $len - 4)) {
  80.                     if($file[0] == "." && $showHidden)
  81.                         array_push($directories, array("name" => $filename, "file" => $file));
  82.                     else if($file[0] != ".")
  83.                         array_push($directories, array("name" => $filename, "file" => $file));
  84.                 } else if($type == "file" || ($type == "dir" && ($appPos == $len - 4))) {
  85.                     if($file[0] == "." && $showHidden)
  86.                         array_push($files, array("name" => $filename, "file" => $file));
  87.                     else if($file[0] != ".")
  88.                         array_push($files, array("name" => $filename, "file" => $file));
  89.                 }
  90.             }    
  91.         }
  92.     
  93.         $response = array(    "template" => "directory.tpl",
  94.                             "directories" => $directories,
  95.                             "files" => $files,
  96.                             "currentDir" => $directory,
  97.                             "user" => $userInfo,
  98.                             "params" => $params,
  99.                         );
  100.         
  101.         return ScriptResponse($response);
  102.         
  103.     } else if(file_exists($directory)) {
  104.         $response = array(
  105.                         "template" => "file.tpl",
  106.                         "dir" => $params["currentDir"],
  107.                         "file" => $params["file"],
  108.                         "filename" => $params["filename"],
  109.                     );
  110.         
  111.         //echo_object(0, $response, "Response");
  112.         
  113.         return ScriptResponse($response);
  114.         
  115.     } else {
  116.         return ScriptErrorResponse("", "File not found.");
  117.     }
  118.     
  119.     return $response;
  120. }
  121.  
  122.  
  123. function EditFile($params)
  124. {
  125.     $fileCmd = $params["fileCmd"];
  126.     $directory = $params["dir"];
  127.     $file = $params["file"];
  128.     $filename = $params["filename"];
  129.     
  130.     $response = array(
  131.                         "template" => "file.tpl",
  132.                         "dir" => $directory,
  133.                         "file" => $file,
  134.                         "filename" => $filename,
  135.                     );
  136.     
  137.     if(!$fileCmd) {
  138.         
  139.     
  140.     } else {
  141.         $filePath = $directory . $file;
  142.         
  143.         switch($fileCmd) {
  144. /*
  145.             case "print":    //$result = exec("osascript -e 'tell application \"Finder\" to print alias (POSIX file \"$filePath\" as text)'");
  146.                             $result = exec("osascript -e 'tell application \"Finder\" to print alias (POSIX file \"$filePath\" as text)' -e 'return \"1\"'");
  147.                             //echo "Result: $result";
  148.                             if($result == 1) {
  149.                                 $response["err"] = "print_success";
  150.                             } else {
  151.                                 $response["err"] = "print_err";
  152.                             }
  153.                             break;
  154. */        
  155.             case "download":    if(file_exists($filePath)) {
  156.                                     if($fp = fopen($filePath, "r")) {
  157.                                         $size = filesize($filePath);
  158.                                         $fname = basename($filePath);
  159.                                         
  160.                                         header("Pragma: ");
  161.                                         header("Cache-Control: ");
  162.                                         header("Content-type: application/octet-stream");
  163.                                         header("Content-Disposition: attachment; filename=\"" . $fname . "\"");
  164.                                         header("Content-length: $size");
  165.                                         
  166.                                         while(!feof($fp)) {
  167.                                             $buffer = fread($fp, 2048);
  168.                                             print $buffer;
  169.                                         }
  170.                                         fclose($fp);
  171.                                         exit;
  172.                                     }
  173.                                 } else {
  174.                                     die("File not found");
  175.                                 }
  176.                                 
  177.                                 break;
  178.                             
  179.             default:        echo "$fileCmd<br />";
  180.                             break;
  181.         }
  182.     }
  183.  
  184.     
  185.     
  186.     return ScriptResponse($response);
  187. }
  188.  
  189. ?>